//引入axios核心库
import axios from "axios"
import qsString from "querystring"
//设置基础地址
let baseUrl = "/api"




//响应拦截器
axios.interceptors.response.use(res => {
    return res.data
})



//导出get方法
const get = ((url, params = {}) => {
    //利用Promise返回成功和失败的结果
    return new Promise((resolve, reject) => {
        axios.get(baseUrl + url, { params }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        })
    })
})

//导出axios.post方法
const post = (url, params = {}, isFile = false) => {
    let data = {};
    if (isFile) {
        //如果有上传文件转化成为FormData格式
        data = new FormData();
        for (let i in params) {
            data.append(i, params[i]);
        }
    } else {
        // 没有上传文件转化成为查询字符串格式
        data = qsString.stringify(params);
    }
    return new Promise((resolve, reject) => {
        axios.post(baseUrl + url, data)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}
export { get, post }